package com.tchintech.pay.view.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.tchintech.pay.R;
import com.tchintech.pay.delegate.fragment.MainFragmentDelegate;

import com.tchintech.pay.frame.presenter.FragmentPresenter;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.net.OkhttpClientTest;
import com.tchintech.pay.utils.AppConst;
import com.tchintech.pay.utils.zxing.android.CaptureActivity;
import com.tchintech.pay.view.activity.BankListActivity;
import com.tchintech.pay.view.activity.CodeReceiveActivity;
import com.tchintech.pay.view.activity.QuickPayActivity;
import com.tchintech.pay.view.activity.QuickPayOneActivity;
import com.tchintech.pay.view.activity.QuickPaytwoActivity;
import com.tchintech.pay.view.activity.ScanOrderActivity;
import com.tchintech.pay.view.activity.ShopActiviy;
import com.tchintech.pay.view.activity.SpeedyActivity;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;

import static android.app.Activity.RESULT_OK;

/**
 * Created by Administrator on 2017/7/10 0010.
 */

public class MainFragment extends FragmentPresenter<MainFragmentDelegate> implements View.OnClickListener {
    private Intent intent;
    private static final int REQUEST_CODE_SCAN = 1575;
    private static final String DECODED_CONTENT_KEY = "codedContent";
    //保存当前页面
    public static List<Activity> listA;
    private Handler handler;
    private Map<String, String> map;
    private String grades_url = "Login.getGrades";

    @Override
    protected void initView() {
        super.initView();
        viewDelegate.setOnClickListener(this, R.id.real_quickpay, R.id.shop, R.id.loan, R.id.appcard, R.id.speedy);
        handler();
        setGrades();
    }


    @Override
    protected void initData() {
        super.initData();
        intent = new Intent();
        listA = new ArrayList<Activity>();
    }

    /**
     * 查询用户当前等级，并存入用户等级表
     */
    private void setGrades() {
        map = new HashMap<String, String>();
        map.put("user_id", AppConst.getUser().getId());
        // showProgressDialog("", "加载中，请稍后...");
        new OkhttpClientTest().path(getActivity(), handler, grades_url, map, HttpCode.GRADES_USER);

    }

    private void handler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                hideProgressDialog();
                switch (msg.what) {
                    case HttpCode.networkabnormal:
                        showMsg("当前网络不可用");
                        break;
                    case HttpCode.http_failed:
                        showMsg("连接失败");
                        break;
                    case HttpCode.http_timeout:
                        showMsg("请求超时");
                        break;
                    case HttpCode.GRADES_USER:
                        try {
                            JSONObject json = new JSONObject(msg.obj.toString());
                            if (json.getString("ret").equals("1")) {
                                String grades = json.getJSONObject("data").getString("gradesid");
                                //获取当前用户等级
                                AppConst.getUser().setGradesid(grades);
                            } else {
                                showMsg(json.getString("msg"));
                            }


                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                }
            }
        };
    }




    @Override
    protected Class<MainFragmentDelegate> getDelegateClass() {
        return MainFragmentDelegate.class;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {



           /* case R.id.real_sao:
                //扫一扫
                 intent = new Intent(getActivity(), CaptureActivity.class);
                startActivityForResult(intent, REQUEST_CODE_SCAN);
                break;
            case R.id.real_code:
                //二维码
                intent.setClass(getActivity(),CodeReceiveActivity.class);
                startActivity(intent);
                break;*//* case R.id.real_sao:
                //扫一扫0

                 intent = new Intent(getActivity(), CaptureActivity.class);
                startActivityForResult(intent, REQUEST_CODE_SCAN);
                break;
            case R.id.real_code:
                //二维码
                intent.setClass(getActivity(),CodeReceiveActivity.class);
                startActivity(intent);
                break;*/
            case R.id.speedy:
                //便民服务
                intent.setClass(getActivity(), SpeedyActivity.class);
                startActivity(intent);
                break;
            case R.id.real_quickpay:
                //快捷支付

                intent.setClass(getActivity(), QuickPayActivity.class);
                startActivity(intent);
                break;
            case R.id.shop:
                //商城
                intent.setClass(getActivity(), ShopActiviy.class);
                startActivity(intent);
                break;
            case R.id.appcard:
                //信用卡申请
                intent.setClass(getActivity(), BankListActivity.class);
                startActivity(intent);
                break;

            default:
                showMsg("正在开发中，敬请期待...");
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_SCAN && resultCode == RESULT_OK) {
            String c = data.getStringExtra(DECODED_CONTENT_KEY);
            //showMsg("解码结果：" + c);
            //Intent intent = new Intent(getActivity(), ScanOrderActivity.class);
            intent.setClass(getActivity(), ScanOrderActivity.class);
            intent.putExtra("AuthCode", c);
            startActivity(intent);
        }

    }
}
